package com.st.mall.address.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.st.mall.address.mapper.IAddressMapper;
import com.st.mall.address.model.AddressModel;
import com.st.mall.address.service.IAddressService;
import com.st.mall.user.mapper.IUserMapper;

@Service
public class AddressServiceImpl implements IAddressService {
	@Autowired
	private IAddressMapper mapper;
	@Autowired
	private IUserMapper userMapper;

	/**
	 * A：该用户不存在 B：该用户已有收货地址,完成修改 C：添加成功 D：添加失败
	 */
	@Override
	public String insert(AddressModel addrModel) {
		String userCode = addrModel.getUserCode();
		if (userMapper.selectId(userCode) == null) {
			return "A";
		}
		if(mapper.selectId(addrModel) !=null) {
			mapper.update(addrModel);
			return "B";
		}
		int result = mapper.insert(addrModel);
		if (result == 1)
			return "C";
		return "D";
	}

	@Override
	public AddressModel selectId(AddressModel addrModel) {
		AddressModel model = mapper.selectId(addrModel);
		return model;
	}


	@Override
	public int delete(AddressModel addrModel) {
		int result = mapper.delete(addrModel);
		return result;
	}

	@Override
	public int update(AddressModel addrModel) {
		int result = mapper.update(addrModel);
		return result;
	}

	@Override
	public List<AddressModel> selectAll(AddressModel addrModel) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<AddressModel> selectModel(AddressModel addrModel) {
		String code = addrModel.getUserCode();
		if (code != null) {
			addrModel.setUserCode("%" + code + "%");
		}
		String name = addrModel.getName();
		if (name != null) {
			addrModel.setName("%" + name + "%");
		}
		List<AddressModel> list = mapper.selectModel(addrModel);
		return list;
	}

	@Override
	public int selectCount(AddressModel addrModel) {
		int result = mapper.selectCount(addrModel);
		return result;
	}

}
